package com.example.kp.mci;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;

public class EditSettingsActivity extends Activity {

	private Spinner userSpinner;
	private Spinner fontSizeSpinner;
	private Spinner fontSpinner;
	private Spinner lineSpacingSpinner;
	private Spinner colorThemeSpinner;

	private CheckBox speechOutCheckbox;
	private SeekBar volumeSeekBar;
	private SeekBar speedSeekBar;
	private CheckBox subtitleCheckbox;

	private CheckBox speechInCheckbox;
	private CheckBox feedbackCheckbox;
	private CheckBox poiListOnCheckbox;
	private RadioGroup feedbackRadioGroup;
	private RadioButton subtitleFeedbackRadio;
	private RadioButton speechFeedbackRadio;

	private ArrayList<String> userList = new ArrayList<String>();
	private boolean refresh= false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_edit_settings);

		userSpinner = (Spinner) this.findViewById(R.id.userSpinner);
		fontSizeSpinner = (Spinner) this.findViewById(R.id.fontSizeSpinner);
		fontSpinner = (Spinner) this.findViewById(R.id.fontSpinner);
		lineSpacingSpinner = (Spinner) this
				.findViewById(R.id.lineSpacingSpinner);
		colorThemeSpinner = (Spinner) this.findViewById(R.id.colorThemeSpinner);
		speechOutCheckbox = (CheckBox) this
				.findViewById(R.id.speechOutCheckbox);
		volumeSeekBar = (SeekBar) this.findViewById(R.id.volumeSeekBar);
		volumeSeekBar.setMax(100);
		speedSeekBar = (SeekBar) this.findViewById(R.id.speedSeekBar);
		speedSeekBar.setMax(150);
		poiListOnCheckbox = (CheckBox) this
				.findViewById(R.id.poiListOnCheckbox);
		subtitleCheckbox = (CheckBox) this.findViewById(R.id.subtitleCheckbox);
		speechInCheckbox = (CheckBox) this.findViewById(R.id.speechInCheckbox);
		feedbackCheckbox = (CheckBox) this.findViewById(R.id.feedbackCheckbox);
		feedbackRadioGroup = (RadioGroup) this
				.findViewById(R.id.feedbackRadioGroup);
		speechFeedbackRadio = (RadioButton) this
				.findViewById(R.id.speechFeedbackRadio);
		subtitleFeedbackRadio = (RadioButton) this
				.findViewById(R.id.subtitleFeedbackRadio);
		PreferencesManager.getInstance().initializeDatabaseValues();
		PreferencesManager.getInstance().setUpdateDB(false);

		OnTouchListener listenRefresh = new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				refresh=true;
				return false;
			}
		};
		fontSizeSpinner.setOnTouchListener(listenRefresh);
		fontSpinner.setOnTouchListener(listenRefresh);
		lineSpacingSpinner.setOnTouchListener(listenRefresh);
		colorThemeSpinner.setOnTouchListener(listenRefresh);
		speechOutCheckbox.setOnTouchListener(listenRefresh);
		volumeSeekBar.setOnTouchListener(listenRefresh);
		speedSeekBar.setOnTouchListener(listenRefresh);
		poiListOnCheckbox.setOnTouchListener(listenRefresh);
		subtitleCheckbox.setOnTouchListener(listenRefresh);
		speechInCheckbox.setOnTouchListener(listenRefresh);
		feedbackCheckbox.setOnTouchListener(listenRefresh);
		subtitleFeedbackRadio.setOnTouchListener(listenRefresh);
		speechFeedbackRadio.setOnTouchListener(listenRefresh);
		
		
		userList.add("Benutzerdefiniert");
		for (int i = 0; i < UserParser.getInstance(getApplicationContext())
				.getUserList().size(); i++) {
			userList.add(UserParser.getInstance(getApplicationContext())
					.getUserList().get(i).getName());
		}
		ArrayAdapter<String> userProfileAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_dropdown_item, userList);
		userSpinner.setAdapter(userProfileAdapter);
		userSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String profile = userSpinner.getSelectedItem().toString();
				PreferencesManager.getInstance().setUserProfile(profile);
				UserParser up = UserParser.getInstance(getApplicationContext());
				ArrayList<User> userList = up.getUserList();
				for (int i = 0; i < userList.size(); i++) {
					if (userList.get(i).getName()
							.equals((String) userSpinner.getSelectedItem())) {
						userList.get(i).updatePreferences();

					}
					initializeWidgetValues();

				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});

		ArrayAdapter<CharSequence> fontSizeAdapter = ArrayAdapter
				.createFromResource(this, R.array.font_size_values,
						android.R.layout.simple_spinner_item);
		fontSizeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		fontSizeAdapter.setDropDownViewResource(R.layout.spinner_layout);
		fontSizeSpinner.setAdapter(fontSizeAdapter);
		fontSizeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int size = Integer.parseInt((String) fontSizeSpinner
						.getSelectedItem());
				PreferencesManager.getInstance().setFontSize(size);
				refreshUserProfile();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});

		ArrayAdapter<CharSequence> fontAdapter = ArrayAdapter
				.createFromResource(this, R.array.font_values,
						android.R.layout.simple_spinner_item);
		fontAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		fontAdapter.setDropDownViewResource(R.layout.spinner_layout);
		fontSpinner.setAdapter(fontAdapter);
		fontSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int font = (Integer) fontSpinner.getSelectedItemPosition();
				PreferencesManager.getInstance().setFont(font);
				refreshUserProfile();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});

		ArrayAdapter<CharSequence> lineSpacingAdapter = ArrayAdapter
				.createFromResource(this, R.array.line_spacing_values,
						android.R.layout.simple_spinner_item);
		lineSpacingAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lineSpacingAdapter.setDropDownViewResource(R.layout.spinner_layout);
		lineSpacingSpinner.setAdapter(lineSpacingAdapter);
		lineSpacingSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						int spacing = (Integer) lineSpacingSpinner
								.getSelectedItemPosition();
						PreferencesManager.getInstance()
								.setLineSpacing(spacing);
						refreshUserProfile();
					}

					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
					}
				});

		ArrayAdapter<CharSequence> colorThemeAdapter = ArrayAdapter
				.createFromResource(this, R.array.color_theme_values,
						android.R.layout.simple_spinner_item);
		colorThemeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		colorThemeAdapter.setDropDownViewResource(R.layout.spinner_layout);
		colorThemeSpinner.setAdapter(colorThemeAdapter);
		colorThemeSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						int theme = (Integer) colorThemeSpinner
								.getSelectedItemPosition();
						PreferencesManager.getInstance().setColorTheme(theme);
						refreshUserProfile();
					}

					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
					}
				});

		initializeWidgetValues();
		poiListOnCheckbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						PreferencesManager.getInstance()
								.setPoiListOn(isChecked);
						refreshUserProfile();
					}
				});
		speechOutCheckbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						PreferencesManager.getInstance().setSpeechOutputOn(
								isChecked);
						if (isChecked) {
							volumeSeekBar.setEnabled(true);
							speedSeekBar.setEnabled(true);
							subtitleCheckbox.setEnabled(true);
						} else {
							volumeSeekBar.setEnabled(false);
							speedSeekBar.setEnabled(false);
							subtitleCheckbox.setEnabled(false);
						}
						refreshUserProfile();
					}
				});

		volumeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				PreferencesManager.getInstance().setVolume(
						volumeSeekBar.getProgress());
				refreshUserProfile();
			}
		});

		speedSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				PreferencesManager.getInstance().setSpeed(
						speedSeekBar.getProgress() + 50);
				refreshUserProfile();
			}
		});

		subtitleCheckbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						PreferencesManager.getInstance().setSubtitlesOn(
								isChecked);
						refreshUserProfile();
					}
				});

		speechInCheckbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						PreferencesManager.getInstance().setSpeechInputOn(
								isChecked);
						refreshUserProfile();
					}
				});

		feedbackCheckbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						PreferencesManager.getInstance().setFeedbackOn(
								isChecked);
						refreshUserProfile();
						if (isChecked) {
							subtitleFeedbackRadio.setEnabled(true);
							speechFeedbackRadio.setEnabled(true);
						} else {
							subtitleFeedbackRadio.setEnabled(false);
							speechFeedbackRadio.setEnabled(false);
						}
					}
				});

		feedbackRadioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					public void onCheckedChanged(RadioGroup rGroup,
							int checkedId) {
						refreshUserProfile();
						if (checkedId == R.id.subtitleFeedbackRadio) {
							PreferencesManager.getInstance()
									.setSpeechFeedbackOn_subtitleFeedbackOff(
											false);
						}
						if (checkedId == R.id.speechFeedbackRadio) {
							PreferencesManager.getInstance()
									.setSpeechFeedbackOn_subtitleFeedbackOff(
											true);
						}
					}
				});
		PreferencesManager.getInstance().setUpdateDB(true);

	}

	private void initializeWidgetValues() {

		for (int i = 0; i < userList.size(); i++) {
			if (userList.get(i).equals(
					PreferencesManager.getInstance().getUserProfile()))
				userSpinner.setSelection(i);
		}

		fontSizeSpinner.setSelection(PreferencesManager.getInstance()
				.getFontSize() / 5 - 1);
		fontSpinner.setSelection(PreferencesManager.getInstance().getFont());
		lineSpacingSpinner.setSelection(PreferencesManager.getInstance()
				.getLineSpacing());
		colorThemeSpinner.setSelection(PreferencesManager.getInstance()
				.getColorTheme());
		poiListOnCheckbox.setChecked(PreferencesManager.getInstance()
				.isPoiListOn());
		speechOutCheckbox.setChecked(PreferencesManager.getInstance()
				.isSpeechOutputOn());
		subtitleCheckbox.setChecked(PreferencesManager.getInstance()
				.isSubtitlesOn());
		speechInCheckbox.setChecked(PreferencesManager.getInstance()
				.isSpeechInputOn());
		feedbackCheckbox.setChecked(PreferencesManager.getInstance()
				.isFeedbackOn());
		if (PreferencesManager.getInstance().isFeedbackOn()) {
			subtitleFeedbackRadio.setEnabled(true);
			speechFeedbackRadio.setEnabled(true);
		} else {
			subtitleFeedbackRadio.setEnabled(false);
			speechFeedbackRadio.setEnabled(false);
		}

		if (PreferencesManager.getInstance()
				.isSpeechFeedbackOn_subtitleFeedbackOff()) {
			subtitleFeedbackRadio.setChecked(false);
			speechFeedbackRadio.setChecked(true);
		} else {
			subtitleFeedbackRadio.setChecked(true);
			speechFeedbackRadio.setChecked(false);
		}

		volumeSeekBar.setProgress(PreferencesManager.getInstance().getVolume());
		speedSeekBar
				.setProgress(PreferencesManager.getInstance().getSpeed() - 50);

		if (PreferencesManager.getInstance().isSpeechOutputOn()) {
			volumeSeekBar.setEnabled(true);
			speedSeekBar.setEnabled(true);
			subtitleCheckbox.setEnabled(true);
		} else {
			volumeSeekBar.setEnabled(false);
			speedSeekBar.setEnabled(false);
			subtitleCheckbox.setEnabled(false);
		}
	}

	public void refreshUserProfile() {
		PreferencesManager pref = PreferencesManager.getInstance();
		for (int i = 0; i < UserParser.getInstance(getApplicationContext())
				.getUserList().size(); i++) {
			User user = UserParser.getInstance(getApplicationContext())
					.getUserList().get(i);
//			if (!pref.getUserProfile().equals("Benutzerdefiniert")
//					&& pref.getUserProfile().equals(user.getName())
//					&& !(pref.getColorTheme() == user.getColorTheme()
//							&& pref.getFont() == user.getFont()
//							&& pref.getFontSize() == user.getFontSize()
//							&& pref.getLineSpacing() == user.getLineSpacing()
//							&& pref.getSpeed() == user.getSpeed()
//							&& pref.getVolume() == user.getVolume()
//							&& pref.isFeedbackOn() == user.isFeedbackOn()
//							&& pref.isSpeechFeedbackOn_subtitleFeedbackOff() == user
//									.isSpeechFeedbackOn_subtitleFeedbackOff()
//							&& pref.isSpeechInputOn() == user.isSpeechInputOn()
//							&& pref.isSpeechOutputOn() == user
//									.isSpeechOutputOn() && pref.isSubtitlesOn() == user
//							.isSubtitlesOn())) {
			if(refresh){
				PreferencesManager.getInstance().setUserProfile(
						"Benutzerdefiniert");
				userSpinner.setSelection(0);
				refresh=false;
			}
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		PreferencesManager.getInstance().initializeDatabaseValues();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_edit_settings, menu);
		return true;
	}

}
